import Vue from 'vue'
import {cloneDeep} from 'lodash'

const UPDATE_ISCOLLAPSE = 'UPDATE_ISCOLLAPSE'
const CHECK_CARD = 'CHECK_CARD'
const UPDATE_ISDETAIL = 'UPDATE_ISDETAIL'
const ADDACCESS = 'ADDACCESS'
const UPDATE_DRAWER = 'UPDATE_DRAWER'
const UPDATE_EDIT = 'UPDATE_EDIT'
const UPDATE_TREE = 'UPDATE_TREE'
const state = {
  isCollapse: false,
  isShowDetial: false,
  isShowDrawer: false,
  isShowEdit: false,
  treeChange: null
}
const mutations = {
  [UPDATE_ISCOLLAPSE] (state, data) {
    state.isCollapse = data === '1' ? false : !state.isCollapse
  },
  [UPDATE_ISDETAIL] (state, data) {
    state.isShowDetial = data === '1' ? false : !state.isShowDetial
  },
  [UPDATE_DRAWER] (state, data) {
    state.isShowDrawer = data
  },
  [UPDATE_EDIT] (state, data) {
    state.isShowEdit = data
  },
  [UPDATE_TREE] (state, data) {
    state.treeChange = data
  }
}
const actions = {
  [CHECK_CARD] ({commit, dispatch}, data) {
    let _data = cloneDeep(data)
    return new Promise((resolve, reject) => {
      Vue.req.post('/http/saas/employee/checkDocumentNo.json', _data, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  [ADDACCESS] ({commit, dispatch}, data) {
    let _data = cloneDeep(data)
    return new Promise((resolve, reject) => {
      Vue.req.post('/http/saas/access/addAccess.json', _data, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'jsonType': 'stringify'
        }
      }).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
